package ui;


import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JProgressBar;







import org.apache.log4j.Logger;

import connector.MsgProcess;
import core.Client;
import core.MinaServer;
import core.MinaSocket;
import core.ServerMain;
import resourse.ResourseMannager;
import sound.SoundPlay;

/**
 * 
 * @author 胡海亮  QQ:249782944
 * 学校:中南大学 商学院
 * EnterpriseIMServer :企业即时通讯
 * Apr 27, 2010
 * 
 *  加载系统的界面，具体工作如下：
 * 
 * 1：初始化EIM系统主界面
 * 2：初始化EIM的桌面右下角的系统托盘类
 * 3：初始化服务器配置信息
 * 4：初始化配置信息 数据库
 */
public class LoadingFrame extends BaseFrame implements Runnable {
	/**
	 */
	private static final long serialVersionUID = 1L;
	 /** 加载进度条*/
	private JProgressBar jProgressBar1 = new JProgressBar();
	 /** 加载页面*/
	private JLabel indexLabel = new JLabel();
	
	public LoadingFrame(){
	       
	        indexLabel.setText("indexLabel");
	        Icon icon=ResourseMannager.getIcon("Index.jpg");
	        indexLabel.setIcon(icon);
	        
	        jProgressBar1.setBorder(null);
	        jProgressBar1.setStringPainted(true);
	        jProgressBar1.setBorderPainted(false);
	        jProgressBar1.setString("系统加栽中......请稍等！．．");
	        jProgressBar1.setBackground(Color.white);
	        jProgressBar1.setForeground(new java.awt.Color(102, 204, 255));
	        //368*224
	    
	        this.getContentPane().add(indexLabel,BorderLayout.NORTH);
	        this.getContentPane().add(jProgressBar1, BorderLayout.SOUTH);
	        
	        this.setSize(icon.getIconWidth(),icon.getIconHeight()+22);
	        this.setUndecorated(true);
	        //this.setAlwaysOnTop(true);
	}
	
	public void setProgressText(String info){
		jProgressBar1.setString(info);
	}
	
	public void run() {
		setProgressText("初始化服务配置信息.....");
		
	  //try {
		
		setProgressText("初始化服务器主界面.....");
		/*
		ServerDisplay.getInstance().init();
		try {
			if(MinaServer.serverStart()){
				
				logger.debug("服务器启动成功......");
				
			}else
			{
				
				logger.debug("服务器启动失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.debug("服务器启动异常");
			e.printStackTrace();
		}
		ServerDisplay.getInstance().updateComponents();
		*/
		
		
		ServerMainFrame mainFrame=ServerMainFrame.getInstance();
		Thread mainFrameThread=new Thread(mainFrame);
		mainFrameThread.start();
		
		for (int i = 0; i < 10; i++) {
				//Thread.currentThread().sleep(20);
            jProgressBar1.setValue(jProgressBar1.getValue() + 1);
        }
        
		//EIMServer sever=EIMServer.getInstance();
		setProgressText("初始化服务器主后台系统.....");
		MinaServer minaServer = MinaServer.getInstance();
		/*
		try {
			if(MinaSocket.serverStart()){
				
				logger.debug("服务器启动成功......");
				
			}else
			{
				
				logger.debug("服务器启动失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.debug("服务器启动异常");
			e.printStackTrace();
		}
		*/
		
		setProgressText("初始化任务处理队列.....");
		MsgProcess process = new MsgProcess();
		Thread msgProcessThread=new Thread(process);
		msgProcessThread.start();
		
		
		
		setProgressText("连接上位机服务器.....");
		Client client = new Client();
		Thread clientThread=new Thread(client);
		clientThread.start();
		
		for (int i = 10; i < 70; i++) {
			//Thread.currentThread().sleep(20);
            jProgressBar1.setValue(jProgressBar1.getValue() + 1);
        }
        
		ServerTrayIcon deskTopIcon=ServerTrayIcon.getInstanse();
		setProgressText("初始化服务器托盘.....");
		Thread deskTopIconThread=new Thread(deskTopIcon);
		deskTopIconThread.start();
		
		for (int i = 70; i < 80; i++) {
			//Thread.currentThread().sleep(20);
            jProgressBar1.setValue(jProgressBar1.getValue() + 1);
        }
		
		//setProgressText("初始化服务配置信息.....");
		//Client.startClient();
		/*
		for (int i = 80; i < 101; i++) {
			Thread.currentThread().sleep(20);
            jProgressBar1.setValue(jProgressBar1.getValue() + 1);
        }
        */
		this.dispose();
		
		
		//SoundPlay.playStartSound();
       // } 
	  /*
	  catch (InterruptedException e) {
			e.printStackTrace();
	    }
	    */
	}
	
	public static void main(String[] args) {
	    
	    LoadingFrame loadingFrame=new LoadingFrame();
	    loadingFrame.setLocationRelativeTo(null);
	    loadingFrame.setVisible(true);
	    Thread index = new Thread(loadingFrame,"MiniServer");
	    index.start();
	}
	
}
